﻿using System;
using System.Data;
using System.Text;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;

namespace GUI.Controls.News
{
    public partial class RelateNews : System.Web.UI.UserControl
    {
        private const string Format = "<li>{3}<a href=\"{0}\" title=\"{2}\">{1}</a></li>";

        private const string NoAvatarUlFormat = "<ul class=\"relation_news clearfix\">{0}</ul>";
        private const string HasAvatarUlFormat = "<ul class=\"relation_news clearfix\">{0}</ul>";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        internal bool DataBind(DataTable dt, bool displayAvatar = false, int imageWidth = 0, int imageHeight = 0)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new StringBuilder();
                foreach (DataRow r in dt.Rows)
                {
                    var avatar = string.Empty;
                    if (displayAvatar)
                        avatar = PresentationUtils.Photo.BuildNewsAvatar(r, imageWidth, imageHeight, lazyLoad: true);
                    sb.AppendFormat(Format, r[SqlConstants.NewsPublish.Url],
                                    r[SqlConstants.NewsPublish.Title],
                                    r[SqlConstants.NewsPublish.EncodedTitle],
                                    avatar);
                }
                ltrData.Text = string.Format(
                    displayAvatar ? HasAvatarUlFormat : NoAvatarUlFormat,
                    sb
                    );
                return true;
            }
            return false;
        }
    }
}